﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace CSharpExt.Xml
{
    public static class XmlExtensions
    {
        #region XmlDocument Extension

        public static XmlDocument LoadFile(this XmlDocument doc, String path)
        {
            using (Stream s = File.OpenRead(path))
            {
                doc.Load(s);
            }
            return doc;
        }


        public static int GetAttributeValue(this XmlElement elt, String attribute, int defaultValue)
        {
            String value = elt.GetAttribute(attribute);
            return String.IsNullOrEmpty(value) ? defaultValue : int.Parse(value);
        }

        public static bool GetAttributeValue(this XmlElement elt, String attribute, bool defaultValue)
        {
            String value = elt.GetAttribute(attribute);
            return String.IsNullOrEmpty(value) ? defaultValue : bool.Parse(value);
        }

        public static String GetAttributeValue(this XmlElement elt, String attribute, String defaultValue)
        {
            String value = elt.GetAttribute(attribute);
            return String.IsNullOrEmpty(value) ? defaultValue : value;
        }

        #endregion
    }
}
